package location.pokemon.com.pokemongolocation.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;

import java.lang.reflect.Field;
import java.util.concurrent.Callable;

import location.pokemon.com.pokemongolocation.R;

/**
 * Created by lwtor on 16/11/23.
 */

public class FloatView extends LinearLayout implements View.OnClickListener {


    private float mXInView;
    private float mYInView;

    private float mXInScreen;
    private float mYInScreen;

    private int mStatusBarHeight;

    private WindowManager windowManager;
    private WindowManager.LayoutParams mParams;

    private FloatClickListener mListener;

    private Button mLeftTopTv;
    private Button mRightTopTv;

    public FloatView(Context context) {
        this(context, null);
    }

    public FloatView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FloatView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        LayoutInflater.from(context).inflate(R.layout.window_layout, this);
        initView();
    }

    public void setOnFloatClickListener(FloatClickListener listener) {
        this.mListener = listener;
    }

    public void setLeftTopText(String text) {
        mLeftTopTv.setText(text);
    }

    public void setRightTopText(String text) {
        mRightTopTv.setText(text);
    }

    private void initView() {

        mLeftTopTv = (Button) findViewById(R.id.auto);
        mRightTopTv = (Button) findViewById(R.id.pause);

        findViewById(R.id.north).setOnClickListener(this);
        findViewById(R.id.south).setOnClickListener(this);
        findViewById(R.id.east).setOnClickListener(this);
        findViewById(R.id.west).setOnClickListener(this);
        findViewById(R.id.auto).setOnClickListener(this);
        findViewById(R.id.pause).setOnClickListener(this);



    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i("lwtor", "on touch event:" + event.getAction() + "," + event.getX() + ":" + event.getY());
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mXInView = event.getX();
                mYInView = event.getY();
                mXInScreen = event.getRawX();
                mYInScreen = event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                mXInScreen = event.getRawX();
                mYInScreen = event.getRawY() - getStatusBarHeight();
                // 手指移动的时候更新小悬浮窗的位置
                updateViewPosition();
                break;
            case MotionEvent.ACTION_UP:

                break;
        }

        return super.onTouchEvent(event);
    }

    public void setParams(WindowManager.LayoutParams params) {
        mParams = params;
    }


    private void updateViewPosition() {
        mParams.x = (int) (mXInScreen - mXInView);
        mParams.y = (int) (mYInScreen - mYInView);
        windowManager.updateViewLayout(this, mParams);
    }

    private int getStatusBarHeight() {
        if (mStatusBarHeight == 0) {
            try {
                Class<?> c = Class.forName("com.android.internal.R$dimen");
                Object o = c.newInstance();
                Field field = c.getField("status_bar_height");
                int x = (Integer) field.get(o);
                mStatusBarHeight = getResources().getDimensionPixelSize(x);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return mStatusBarHeight;
    }

    @Override
    public void onClick(View v) {
        if (mListener == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.north:
                mListener.onNorthClick();
                break;
            case R.id.south:
                mListener.onSouthClick();
                break;
            case R.id.east:
                mListener.onEastClick();
                break;
            case R.id.west:
                mListener.onWestClick();
                break;
            case R.id.auto:
                mListener.onLeftTopClick();
                break;
            case R.id.pause:
                mListener.onRightTopClick();
                break;
        }
    }
}
